package com.aurora.oa.message.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.application.MyApplication;
import com.aurora.oa.login.model.UserInfo;
import com.aurora.oa.message.adapter.MessageAdapter;
import com.aurora.oa.message.bean.MessageTypeBean;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.message.presenter.MessageFragmentPresenter;
import com.aurora.oa.message.presenter.MessageFragmentPresenterImpl;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/3/14 0014.
 */
public class MessageFragment extends Fragment implements MessageFragmentView {

    private MessageFragmentPresenter messageFragmentPresenter;
    private TextView tv_person_message_count;
    private RecyclerView rv_message_type_list;
    private SimpleDraweeView sdv_head_image;
    private TextView tv_name;
    private TextView tv_duty;

    private Button bt_sent_message;

    //存储的参数
    private String mParam;

    //获取 fragment 依赖的 Activity，方便使用 Context
    private Activity mAct;

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    break;
            }
        }
    };

    public static MessageFragment newInstance(String param) {
        MessageFragment fragment = new MessageFragment();
        Bundle args = new Bundle();
        args.putString("param", param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mParam = savedInstanceState.getString("param");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mAct = getActivity();
        View view = inflater.inflate(R.layout.fragment_message_main, container, false);
        sdv_head_image = (SimpleDraweeView) view.findViewById(R.id.sdv_mhead_img);
        messageFragmentPresenter = new MessageFragmentPresenterImpl(this);
        tv_person_message_count =(TextView) view.findViewById(R.id.tv_person_message_count);
        rv_message_type_list = (RecyclerView) view.findViewById(R.id.rv_message_type_list);

        tv_name = (TextView) view.findViewById(R.id.tv_user_name);
        tv_duty = (TextView) view.findViewById(R.id.tv_duty);
        bt_sent_message = (Button) view.findViewById(R.id.bt_sent_message);
        bt_sent_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mAct,MessageSendActivity.class));
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        messageFragmentPresenter.showBaseInfo();
        messageFragmentPresenter.requestMessageTypeData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //ButterKnife.reset(this);
    }




    /**
     * 初始消息列表
     * @param messageList
     */
    private void setUpMessageList(final List<MessageTypeBean> messageList){
        final MessageAdapter messageAdapter = new MessageAdapter(mAct,messageList);
        messageAdapter.setOnItemClickListener(new MessageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(mAct, "Click" + messageList.get(position).getType(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(mAct, "LongClick" + messageList.get(position).getType(), Toast.LENGTH_SHORT).show();
            }
        });

        rv_message_type_list.setAdapter(messageAdapter);
        rv_message_type_list.setLayoutManager(new LinearLayoutManager(mAct));
        rv_message_type_list.setItemAnimator(new DefaultItemAnimator());
        rv_message_type_list.addItemDecoration(new ItemDivider().setDividerWith(8).setDividerColor(Color.TRANSPARENT));
        messageAdapter.setOnItemClickListener(new MessageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                Intent intent = new Intent(mAct,MessageListActivity.class);
                intent.putExtra("message_type", messageAdapter.getMessageTypeBean(position).getType());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void refreshMessagePage(List<MessageTypeBean> messageTypeBeanList) {
        setUpMessageList(messageTypeBeanList);
    }

    @Override
    public void showMyTotalCreatMessage(int popcreate) {
        tv_person_message_count.setText("保存"+popcreate+"条");
    }

    @Override
    public void showBaseInfo() {
        UserInfo userInfo =MyApplication.getInstance().getLoginUser();
        sdv_head_image.setImageURI(AuroraConstants.BASE_SERVER_URL+"/"+MyApplication.getInstance().getLoginUser().getHeadimg());
        tv_name.setText(userInfo.getRealname()==null?"":userInfo.getRealname());
        tv_duty.setText(userInfo.getPostname()==null?"":userInfo.getPostname());
    }
}
